package mytaxis.client.login;

import mytaxis.client.R;
import mytaxis.client.app.MyTaxisClientApplication;
import mytaxis.client.menu.Menu;
import mytaxis.client.service.ComService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Index extends Activity {

	EditText txtEmail;
	EditText txtPass;
	Button login;
	Button register;
	ProgressDialog pd;
	Thread serverCall;
	String mail;
	String pass;
	private MyTaxisClientApplication app;
	
	private ComService mBoundService;

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
		    mBoundService = ((ComService.LocalBinder)service).getService();
		}
	
		public void onServiceDisconnected(ComponentName className) {
		    mBoundService = null;
		}
		};
	
		Handler mHandler = new Handler() {
			public void handleMessage(Message msg) {
				boolean result = msg.getData().getBoolean("result");
				if(result){
					Toast.makeText(getApplicationContext(),"Login OK",0).show();
					Intent i = new Intent(Index.this, Menu.class);
					startActivity(i);
					finish();
				}else{
					Toast.makeText(getApplicationContext(),"Login Error: Try Again", Toast.LENGTH_SHORT).show();
					
				}
			};
		};	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initViews();
		startService(new Intent(Index.this, ComService.class));
		bindService(new Intent(this, ComService.class), mConnection, Context.BIND_AUTO_CREATE);
	}

	private void initViews() {
		
		txtEmail = (EditText) findViewById(R.id.EditTextEmail);
		txtPass = (EditText) findViewById(R.id.EditTextPass);
		login = (Button) findViewById(R.id.btnlogin);
		register = (Button) findViewById(R.id.btnregister);
		pd = new ProgressDialog(this);

		login.setOnClickListener(new Button.OnClickListener(){
			
			public void onClick(View v) {
				if(txtEmail.getText().toString().equals("")|| txtPass.getText().toString().equals("") )Toast.makeText(getApplicationContext(), 
						"Empty user or pass", Toast.LENGTH_SHORT).show();
				else if(!txtEmail.getText().toString().contains("@")){
					Toast.makeText(getApplicationContext(), 
							"Incorrect user", Toast.LENGTH_SHORT).show();
				}	
				else{
					mail= txtEmail.getText().toString();
					pass = txtPass.getText().toString();
					pd.setMessage("Connecting to server, this may take a few seconds.");
					pd.setTitle("Login");
					pd.show();
					//Intent i = new Intent(Index.this, Menu.class);
    				//startActivity(i);
					Thread t = new Thread(new Runnable(){
				    		public void run(){
				    			boolean res = mBoundService.login(mail, pass);
			    				Message msg = new Message();
								Bundle b = new Bundle();
								b.putBoolean("result", res);
								msg.setData(b);
								mHandler.sendMessage(msg);
								pd.dismiss();
				    		}
					});
					t.start();
				}
			}
		}
			);
		
		register.setOnClickListener(new Button.OnClickListener(){
			
			public void onClick(View v) {
				
				Intent i = new Intent(Index.this,Register.class);
				startActivity(i);
				
			}
		}
		);
	}

	public ServiceConnection getmConnection() {
		return mConnection;
	}

	public void setmConnection(ServiceConnection mConnection) {
		this.mConnection = mConnection;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(mConnection);
	}
}
			